//
//  SureDingDanController.swift
//  JianTingSwift
//
//  Created by zhibao on 2024/11/22.
//

import UIKit

class SureDingDanController: SuperController {

    var dic = [String:String]()
    var dingDic = [String:Any]()
    var scrollView = UIScrollView()
    var nameField = QMUITextField()
    var phoneField = QMUITextField()
    var shengField = QMUITextField()
    var shiField = QMUITextField()
    var quField = QMUITextField()
    var xiangxiField = QMUITextField()
    var shuNumField = QMUITextField()
    var dayNumField = QMUITextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.gk_navTitle = "确认信息"
        if dingDic.keys.count > 0 {
            dic = dingDic["product"] as! [String : String]
        }
        QMUITips.showLoading(in: self.view)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            QMUITips.hideAllTips()
            self.hideNavLine()
            self.initView()
        }
    }
    
    fileprivate func initView(){
        self.view.addSubview(scrollView)
        let bottomView = UIView()
        bottomView.backgroundColor = .white
        self.view.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self.view)
            make.height.equalTo(pixw(p: 80))
        }
        
        let line = UIView()
        line.backgroundColor = MYColor.color(withHexString: "e5e5e5")
        bottomView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.top.equalTo(bottomView)
            make.height.equalTo(pixw(p: 1))
        }
        
        let zulinBtn = UIButton(type: .custom)
        zulinBtn.setTitleColor(MYColor.color(withHexString: "ffffff"), for: .normal)
        zulinBtn.titleLabel?.font = MY_SCALE_MediumFONT(16)
        zulinBtn.backgroundColor = MYColor.color(withHexString: "#FC4A17")
        zulinBtn.layer.cornerRadius = pixw(p: 22)
        zulinBtn.setTitle("立即预约", for: .normal)
        bottomView.addSubview(zulinBtn)
        zulinBtn.snp.makeConstraints { make in
            make.centerY.centerX.equalTo(bottomView)
            make.width.equalTo(pixw(p: 200))
            make.height.equalTo(pixw(p: 44))
        }
        zulinBtn.addGestureRecognizer(UITapGestureRecognizer(actionBlock: { sender in
            if self.shengField.text?.count ?? 0 < 2 {
                QMUITips.showInfo("请输入省份")
                return
            }
            if self.shiField.text?.count ?? 0 < 2 {
                QMUITips.showInfo("请输入城市")
                return
            }
            if self.quField.text?.count ?? 0 < 2 {
                QMUITips.showInfo("请输入区/县")
                return
            }
            if self.xiangxiField.text?.count ?? 0 < 4 {
                QMUITips.showInfo("请输入具体门牌号")
                return
            }
            
            if Int(self.dayNumField.text ?? "0") ?? 0 < 1 {
                QMUITips.showInfo("请输入租赁天数")
                return
            }
            if Int(self.shuNumField.text ?? "0") ?? 0 < 1 {
                QMUITips.showInfo("请输入租赁台数")
                return
            }
            var dataArray = MYGlobal.share().getData()
            let dic = [
                "name":self.nameField.text!,
                "phone":self.phoneField.text!,
                "sheng":self.shengField.text!,
                "city":self.shiField.text!,
                "xian":self.quField.text!,
                "xiangxi":self.xiangxiField.text!,
                "dayNum":self.dayNumField.text!,
                "shuNum":self.shuNumField.text!,
                "time":Helper.getCurrentTimes("yyyy-MM-dd HH:mm:ss") ?? "暂无",
                "product":self.dic
            ]
            dataArray.append(dic)
            MYGlobal.share().saveData(withDict: dataArray)
            QMUITips.showLoading(in: self.view)
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                QMUITips.hideAllTips()
                QMUITips.showSucceed("预约成功，审核通过后会有专属客服联系您！")
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    QMUITips.hideAllTips()
                    QMUITips.showSucceed("跳转至订单列表")
                    var tabBarController: UITabBarController?
                    if let rootVC = UIApplication.shared.keyWindow?.rootViewController {
                        tabBarController = self.findTabBarController(in: rootVC)
                    }
                    tabBarController?.selectedIndex = 1
                    self.navigationController?.popToRootViewController(animated: false)
                }
            }
            
        }))
        
        
        self.view.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.left.right.equalTo(self.view)
            make.top.equalTo(navigationFullHeight())
            make.bottom.equalTo(bottomView.snp.top)
        }
        scrollView.backgroundColor = MYColor.color(withHexString: "fafafa")
        
        let addressLabel = UILabel()
        addressLabel.text = "收货信息"
        addressLabel.font = MY_SCALE_MediumFONT(16)
        addressLabel.textColor = .black
        scrollView.addSubview(addressLabel)
        addressLabel.snp.makeConstraints { make in
            make.left.top.equalTo(scrollView).offset(pixw(p:20))
            make.right.equalTo(self.view).offset(pixw(p:-20))
        }
        
        scrollView.addSubview(nameField)
        nameField.placeholder = "请输入姓名"
        nameField.textColor = MYBlock
        nameField.layer.cornerRadius = pixw(p:4)
        nameField.backgroundColor = .white
        nameField.textInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        nameField.font = MY_SCALE_MediumFONT(14)
        nameField.snp.makeConstraints { make in
            make.left.equalTo(addressLabel)
            make.top.equalTo(addressLabel.snp.bottom).offset(pixw(p:10))
            make.right.equalTo(self.view.snp.centerX).offset(pixw(p: -10))
            make.height.equalTo(pixw(p:50))
        }
        
        scrollView.addSubview(phoneField)
        phoneField.placeholder = "请输入手机号"
        phoneField.textColor = MYBlock
        phoneField.layer.cornerRadius = pixw(p:4)
        phoneField.backgroundColor = .white
        phoneField.textInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        phoneField.font = MY_SCALE_MediumFONT(14)
        phoneField.snp.makeConstraints { make in
            make.left.equalTo(nameField.snp.right).offset(pixw(p: 20))
            make.right.equalTo(addressLabel)
            make.top.bottom.equalTo(nameField)
            make.height.equalTo(pixw(p:50))
        }
        
        
        
        scrollView.addSubview(shengField)
        shengField.placeholder = "请输入省份"
        shengField.textColor = MYBlock
        shengField.layer.cornerRadius = pixw(p:4)
        shengField.backgroundColor = .white
        shengField.textInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        shengField.font = MY_SCALE_MediumFONT(14)
        shengField.snp.makeConstraints { make in
            make.left.right.equalTo(addressLabel)
            make.top.equalTo(nameField.snp.bottom).offset(pixw(p:10))
            make.height.equalTo(pixw(p:50))
        }
        
        scrollView.addSubview(shiField)
        shiField.placeholder = "请输入城市"
        shiField.textColor = MYBlock
        shiField.layer.cornerRadius = pixw(p:4)
        shiField.backgroundColor = .white
        shiField.textInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        shiField.font = MY_SCALE_MediumFONT(14)
        shiField.snp.makeConstraints { make in
            make.left.right.equalTo(addressLabel)
            make.top.equalTo(shengField.snp.bottom).offset(pixw(p:10))
            make.height.equalTo(pixw(p:50))
        }
        
        scrollView.addSubview(quField)
        quField.placeholder = "请输入县/区"
        quField.textColor = MYBlock
        quField.layer.cornerRadius = pixw(p:4)
        quField.backgroundColor = .white
        quField.textInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        quField.font = MY_SCALE_MediumFONT(14)
        quField.snp.makeConstraints { make in
            make.left.right.equalTo(addressLabel)
            make.top.equalTo(shiField.snp.bottom).offset(pixw(p:10))
            make.height.equalTo(pixw(p:50))
        }
        
        scrollView.addSubview(xiangxiField)
        xiangxiField.placeholder = "请输入详细地址"
        xiangxiField.textColor = MYBlock
        xiangxiField.layer.cornerRadius = pixw(p:4)
        xiangxiField.backgroundColor = .white
        xiangxiField.textInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        xiangxiField.font = MY_SCALE_MediumFONT(14)
        xiangxiField.snp.makeConstraints { make in
            make.left.right.equalTo(addressLabel)
            make.top.equalTo(quField.snp.bottom).offset(pixw(p:10))
            make.height.equalTo(pixw(p:50))
        }
        
        let cpLabel = UILabel()
        cpLabel.text = "商品信息"
        cpLabel.font = MY_SCALE_MediumFONT(16)
        cpLabel.textColor = .black
        scrollView.addSubview(cpLabel)
        cpLabel.snp.makeConstraints { make in
            make.top.equalTo(xiangxiField.snp.bottom).offset(pixw(p:20))
            make.left.equalTo(pixw(p:20))
            make.right.equalTo(self.view).offset(pixw(p:-20))
        }
        
        let projectView = UIView()
        scrollView.addSubview(projectView)
        projectView.backgroundColor = .white
        projectView.layer.cornerRadius = pixw(p:10)
        projectView.snp.makeConstraints { make in
            make.left.right.equalTo(addressLabel)
            make.top.equalTo(cpLabel.snp.bottom).offset(pixw(p:10))
        }
        
        let projectImgView = UIImageView(image: UIImage(named: dic["img"] ?? ""))
        projectImgView.contentMode = .scaleAspectFit
        projectView.addSubview(projectImgView)
        projectImgView.snp.makeConstraints { make in
            make.left.equalTo(pixw(p: 20))
            make.top.equalTo(pixw(p: 20))
            make.width.height.equalTo(pixw(p: 100))
        }

        
        let newIconView = UIImageView(image: UIImage(named: dic["new"] ?? ""))
        projectImgView.addSubview(newIconView)
        newIconView.snp.makeConstraints { make in
            make.left.top.equalTo(projectImgView)
            make.width.equalTo(pixw(p: 50))
            make.height.equalTo(pixw(p: 18))
        }
        
        let nameLabel = UILabel()
        nameLabel.text = dic["name"]
        nameLabel.font = MY_SCALE_MediumFONT(14)
        nameLabel.numberOfLines = 2
        nameLabel.lineBreakMode = .byWordWrapping
        nameLabel.textColor = MYBlock
        projectView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(projectImgView.snp.right);
            make.top.equalTo(projectImgView);
            make.right.equalTo(projectView).offset(pixw(p:-15));
        }
        
        
        let qianPriceLabel = UILabel()
        qianPriceLabel.text = "¥"
        qianPriceLabel.font = MY_SCALE_LightFONT(12)
        qianPriceLabel.textColor = MYRed
        projectView.addSubview(qianPriceLabel)
        qianPriceLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel);
            make.top.equalTo(nameLabel.snp.bottom).offset(pixw(p: 10));
        }
        
        let priceLabel = UILabel()
        priceLabel.text = dic["price"]
        priceLabel.font = MY_SCALE_BoldFont(20)
        priceLabel.textColor = MYRed
        projectView.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(qianPriceLabel.snp.right).offset(0);
            make.bottom.equalTo(qianPriceLabel);
        }
        
        let cpriceLabel = UILabel()
        cpriceLabel.text = "/天"
        cpriceLabel.font = MY_SCALE_LightFONT(12)
        cpriceLabel.textColor = MYRed
        projectView.addSubview(cpriceLabel)
        cpriceLabel.snp.makeConstraints { make in
            make.left.equalTo(priceLabel.snp.right).offset(0);
            make.bottom.equalTo(qianPriceLabel);
        }
        
        let baoYouLabel = UILabel()
        baoYouLabel.text = "   vip独享:\(String(describing: dic["vip"] ?? ""))   "
        baoYouLabel.font = MY_SCALE_RegularFONT(11)
        baoYouLabel.layer.cornerRadius = pixw(p: 3)
        baoYouLabel.layer.masksToBounds = true
        baoYouLabel.backgroundColor = MYColor.color(withHexString: "#E95A27", alpha: 0.1)
        baoYouLabel.textColor = MYRed
        projectView.addSubview(baoYouLabel)
        baoYouLabel.snp.makeConstraints { make in
            make.left.equalTo(qianPriceLabel);
            make.top.equalTo(cpriceLabel.snp.bottom).offset(pixw(p: 4));
            make.height.equalTo(pixw(p: 18))
        }
        
        let dayLabel = UILabel()
        dayLabel.text = "\(String(describing: dic["day"] ?? ""))天起租"
        dayLabel.font = MY_SCALE_RegularFONT(12)
        dayLabel.textColor = MYColor.color(withHexString: "#999999")
        projectView.addSubview(dayLabel)
        dayLabel.snp.makeConstraints { make in
            make.left.equalTo(projectView)
            make.top.equalTo(projectImgView.snp.bottom).offset(pixw(p:20))
            make.bottom.equalTo(pixw(p: -20))
        }
        
        let cdayLabel = UILabel()
        cdayLabel.text = "天数:"
        cdayLabel.font = MY_SCALE_RegularFONT(12)
        cdayLabel.textColor = MYColor.color(withHexString: "#666666")
        projectView.addSubview(cdayLabel)
        cdayLabel.snp.makeConstraints { make in
            make.left.equalTo(dayLabel.snp.right).offset(pixw(p:20));
            make.centerY.equalTo(dayLabel);
        }
        
        projectView.addSubview(dayNumField)
        dayNumField.placeholder = "请输入天数"
        dayNumField.textColor = MYBlock
        dayNumField.text = "5"
        dayNumField.backgroundColor = MYColor.color(withHexString: "fafafa")
        dayNumField.textInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        dayNumField.font = MY_SCALE_MediumFONT(11)
        dayNumField.snp.makeConstraints { make in
            make.left.equalTo(cdayLabel.snp.right)
            make.centerY.equalTo(dayLabel)
            make.height.equalTo(pixw(p:40))
            make.width.equalTo(pixw(p:70))
        }
        
        let cshuNumLabel = UILabel()
        cshuNumLabel.text = "数量:"
        cshuNumLabel.font = MY_SCALE_RegularFONT(12)
        cshuNumLabel.textColor = MYColor.color(withHexString: "#666666")
        projectView.addSubview(cshuNumLabel)
        cshuNumLabel.snp.makeConstraints { make in
            make.left.equalTo(dayNumField.snp.right).offset(pixw(p:20));
            make.centerY.equalTo(dayLabel);
        }
        
        projectView.addSubview(shuNumField)
        shuNumField.placeholder = "请输入数量"
        shuNumField.textColor = MYBlock
        shuNumField.text = "1"
        shuNumField.backgroundColor = MYColor.color(withHexString: "fafafa")
        shuNumField.textInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        shuNumField.font = MY_SCALE_MediumFONT(11)
        shuNumField.snp.makeConstraints { make in
            make.left.equalTo(cshuNumLabel.snp.right)
            make.centerY.equalTo(dayLabel)
            make.height.equalTo(pixw(p:40))
            make.width.equalTo(pixw(p:70))
        }
        
        if dingDic.keys.count > 0 {
            nameField.text = dingDic["name"] as? String
            nameField.isUserInteractionEnabled = false
            phoneField.text = dingDic["phone"] as? String
            phoneField.isUserInteractionEnabled = false
            shengField.text = dingDic["sheng"] as? String
            shengField.isUserInteractionEnabled = false
            shiField.text = dingDic["city"] as? String
            shiField.isUserInteractionEnabled = false
            quField.text = dingDic["xian"] as? String
            quField.isUserInteractionEnabled = false
            xiangxiField.text = dingDic["xiangxi"] as? String
            xiangxiField.isUserInteractionEnabled = false
            dayNumField.text = dingDic["dayNum"] as? String
            dayNumField.isUserInteractionEnabled = false
            shuNumField.text = dingDic["shuNum"] as? String
            shuNumField.isUserInteractionEnabled = false
            zulinBtn.backgroundColor = MYColor.color(withHexString: "999999")
            zulinBtn.setTitle("待审核", for: .normal)
            zulinBtn.isUserInteractionEnabled = false
            
        }else{
            
        }
        
        let tipLabel = UILabel()
        tipLabel.text = "规则说明"
        tipLabel.font = MY_SCALE_BoldFont(17)
        tipLabel.textColor = MYRed
        tipLabel.textAlignment = .center
        scrollView.addSubview(tipLabel)
        tipLabel.snp.makeConstraints { make in
            make.left.right.equalTo(addressLabel)
            make.top.equalTo(projectView.snp.bottom).offset(pixw(p:30))
        }
        
        let contentLabel = UILabel()
        contentLabel.text = "郑重提示:为了明确物品在租赁环节出现损坏而不能明确责任方，再次进行说明:\n1、本平台保证在发货之前认真检查物品完整性、是否可正常使用，并在确认没有问题情况下进行发货。\n2、租赁方收到物品后请立刻进行检查，到货后半个小时内如若没有反馈，本平台将默认物品没有问题。\n3、租赁方在使用过程中如果造成物品损坏，我们将按照当前市场价格进行评估，并向租赁方索赔，索赔金额为:当前市场价格 -已经产生的租赁费。\n4、开通会员可享全平台包邮，开通不同会员商品价格打折不同，具体如下：\n 包周：98折\n包月95折\n包季92折\n包年85折.\n备注：会员到期，商品续费使用恢复原价。"
        contentLabel.font = MY_SCALE_LightFONT(14)
        contentLabel.textColor = MYColor.color(withHexString: "777777")
        contentLabel.numberOfLines = 0
        contentLabel.lineBreakMode = .byWordWrapping
        scrollView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.left.right.equalTo(addressLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(pixw(p:15))
            make.bottom.equalTo(pixw(p:-40))
        }
    }
    
    func findTabBarController(in viewController: UIViewController) -> UITabBarController? {
        if let tabBarController = viewController as? UITabBarController {
            return tabBarController
        } else if let navigationController = viewController as? UINavigationController,
                  let topViewController = navigationController.topViewController {
            return findTabBarController(in: topViewController)
        } else if let presentingViewController = viewController.presentedViewController {
            return findTabBarController(in: presentingViewController)
        } else if let tabBarController = viewController.children.first(where: { $0 is UITabBarController }) as? UITabBarController {
            return tabBarController
        }
        return nil
    }
}
